DAY38:Duplicate Encoder


Posted by birdbirdmurmur on 2023-08-20

題目連結

https://www.codewars.com/kata/54b42f9314d9229fd6000d9c

解法

function duplicateEncode(word) {
    word = word.toLowerCase()
    let result = ''
    let n = {}

    for (const char of word) {
        n[char] ? n[char]++ : n[char] = 1
    }
    for (const char of word) {
        result += n[char] > 1 ? ')' : '('
    }

    return result
}

筆記

跟昨天的題型幾乎一樣
只差在第二個迴圈用for...of
word比較nobject


#javascript #Codewars #for...of #object







Related Posts

所有的函式都是閉包:談 JS 中的作用域與 Closure

所有的函式都是閉包:談 JS 中的作用域與 Closure

2 Basic Mathematical Operations 8kyu

2 Basic Mathematical Operations 8kyu

陪你讀論文 - Part-Aware Data Augmentation for 3D Object Detection in Point Cloud

陪你讀論文 - Part-Aware Data Augmentation for 3D Object Detection in Point Cloud


Comments